/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-07-26
*/
package com.rzico.order.service;

import com.rzico.base.BaseMapper;
import com.rzico.base.impl.BaseServiceImpl;
import com.rzico.basics.entity.Bom;
import com.rzico.basics.entity.BomItem;
import com.rzico.basics.entity.Product;
import com.rzico.basics.service.ProductService;
import com.rzico.exception.CustomException;
import com.rzico.order.entity.*;
import com.rzico.order.model.EmptyBarrelVo;
import io.swagger.annotations.ApiModelProperty;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.rzico.order.mapper.EmptyBarrelMapper;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;

/**
 * <pre>
 * 空桶记录表业务类
 * </pre>
 *
 * @author Rzico Boot
 * @version 1.0
 */
@Service
public class EmptyBarrelService extends BaseServiceImpl<EmptyBarrel, String> {

    @Autowired
    private EmptyBarrelMapper emptyBarrelMapper;

    @Autowired
    private ProductService productService;

    @Autowired
    private ShippingItemService shippingItemService;

    @Autowired
    private WarehouseService warehouseService;

    @Autowired
    private OrderService orderService;

    @Override
    public BaseMapper<EmptyBarrel, String> getMapper() {
        return emptyBarrelMapper;
    }


    public List<EmptyBarrelVo> selectSummaryList(Map<String, Object> params) {
        return emptyBarrelMapper.selectSummaryList(params);
    }


    public List<EmptyBarrel> getItemList(Long shippingId) {
        Map<String,Object> params = new HashMap<>();
        params.put("shippingId",shippingId);
        return emptyBarrelMapper.selectList(params);
    }

    public BigDecimal selectQuantitySum(Long memberId) {
        Map<String,Object> params = new HashMap<>();
        params.put("memberId",memberId);
        BigDecimal amount = emptyBarrelMapper.selectQuantitySum(params);
        if (amount==null) {
            return BigDecimal.ZERO;
        } else {
            return amount;
        }
    }

    //确认出入库
    @Transactional(rollbackFor = Exception.class)
    public void addWarehouse(Warehouse warehouse) {
        for (WarehouseItem warehouseItem:warehouse.getItemList()) {
            if (warehouseItem.getSubType().equals(4)) {
                EmptyBarrel emptyBarrel = new EmptyBarrel();
                emptyBarrel.setCost(warehouseItem.getCost());
                emptyBarrel.setProductId(warehouseItem.getProductId());
                emptyBarrel.setEmptyType(1);
                emptyBarrel.setCreateDate(warehouse.getCreateDate());
                emptyBarrel.setEnterpriseId(warehouse.getEnterpriseId());
                emptyBarrel.setMemberId(warehouse.getMemberId());
                emptyBarrel.setName(warehouseItem.getName());
                emptyBarrel.setSpec(warehouseItem.getSpec());
                emptyBarrel.setUnit(warehouseItem.getUnit());
                emptyBarrel.setThumbnail(warehouseItem.getThumbnail());
                emptyBarrel.setSn(warehouseItem.getSn());
//                emptyBarrel.setShippingId(shipping.getId());
                emptyBarrel.setOrderId(warehouse.getOrderId());
                emptyBarrel.setOrderSn(warehouse.getOrderSn());

               // @ApiModelProperty(value = "单据类型(1.采购入库,2.采购退货,3.调拨入库,4.销售出库,5.销售退货,6.调拨出库,7.盘点损益,8.其他出库)" )

                if (warehouse.getWarehouseType()==1) {
                    emptyBarrel.setEmptyType(0);
                    emptyBarrel.setQuantity(warehouseItem.getQuantity());
                    emptyBarrel.setMemo("采购入库");
                }

                if (warehouse.getWarehouseType()==2) {
                    emptyBarrel.setEmptyType(3);
                    emptyBarrel.setQuantity(BigDecimal.ZERO.subtract(warehouseItem.getQuantity()));
                    emptyBarrel.setMemo("采购退货");
                }

                if (warehouse.getWarehouseType()==4) {
                    emptyBarrel.setEmptyType(1);
                    emptyBarrel.setQuantity(BigDecimal.ZERO.subtract(warehouseItem.getQuantity()));
                    emptyBarrel.setMemo("调拨入库");
                }

                if (warehouse.getWarehouseType()==5) {
                    emptyBarrel.setEmptyType(2);
                    emptyBarrel.setQuantity(warehouseItem.getQuantity());
                    emptyBarrel.setMemo("销售出库");
                }

                if (warehouse.getWarehouseType()==3) {
                    emptyBarrel.setEmptyType(0);
                    emptyBarrel.setQuantity(warehouseItem.getQuantity());
                    emptyBarrel.setMemo("销售退货");
                }

                if (warehouse.getWarehouseType()==6) {
                    emptyBarrel.setEmptyType(3);
                    emptyBarrel.setQuantity(BigDecimal.ZERO.subtract(warehouseItem.getQuantity()));
                    emptyBarrel.setMemo("调拨出库");
                }

                if (warehouse.getWarehouseType()==7) {
                    emptyBarrel.setEmptyType(3);
                    emptyBarrel.setQuantity(BigDecimal.ZERO.subtract(warehouseItem.getQuantity()));
                    emptyBarrel.setMemo("盘点损益");
                }

                if (warehouse.getWarehouseType()==8) {
                    emptyBarrel.setEmptyType(3);
                    emptyBarrel.setQuantity(BigDecimal.ZERO.subtract(warehouseItem.getQuantity()));
                    emptyBarrel.setMemo("其他出库");
                }

//                emptyBarrel.setQuantity(BigDecimal.ZERO.subtract(shippingItem.getQuantity()));
                emptyBarrel.setShopId(warehouse.getShopId());
//                emptyBarrel.setShippingEmployeeId(warehouse.getShippingEmployeeId());
                emptyBarrel.setAudited(1);
                emptyBarrelMapper.insertUseGeneratedKeys(emptyBarrel);

            }
        }

    }


    @Transactional(rollbackFor = Exception.class)
    public void delWarehouse(Warehouse warehouse) {

        EmptyBarrel emptyBarrel = new EmptyBarrel();
        emptyBarrel.setWarehouseId(warehouse.getId());

        emptyBarrelMapper.delete(emptyBarrel);

    }

    //送货
    @Transactional(rollbackFor = Exception.class)
    public void addShipping(Shipping shipping) {

        for (ShippingItem shippingItem:shipping.getItemList()) {
            if (shippingItem.getSubType().equals(4)) {
                //退桶/回桶
                if (shippingItem.getEmptyBarrelQuantity().compareTo(BigDecimal.ZERO)>0) {
                    EmptyBarrel emptyBarrel = new EmptyBarrel();
                    emptyBarrel.setCost(shippingItem.getCost());
                    emptyBarrel.setProductId(shippingItem.getProductId());
                    emptyBarrel.setEmptyType(2);
                    emptyBarrel.setCreateDate(shipping.getCreateDate());
                    emptyBarrel.setEnterpriseId(shipping.getEnterpriseId());
                    emptyBarrel.setMemberId(shipping.getMemberId());
                    emptyBarrel.setName(shippingItem.getName());
                    emptyBarrel.setSpec(shippingItem.getSpec());
                    emptyBarrel.setUnit(shippingItem.getUnit());
                    emptyBarrel.setThumbnail(shippingItem.getThumbnail());
                    emptyBarrel.setSn(shippingItem.getSn());
                    emptyBarrel.setShippingId(shipping.getId());
                    emptyBarrel.setOrderId(shipping.getOrderId());
                    emptyBarrel.setOrderSn(shipping.getOrderSn());
                    emptyBarrel.setQuantity(shippingItem.getEmptyBarrelQuantity());
                    emptyBarrel.setShopId(shipping.getShopId());
                    emptyBarrel.setShippingEmployeeId(shipping.getShippingEmployeeId());
                    emptyBarrel.setAudited(0);
                    emptyBarrel.setShippingItemId(shippingItem.getId());
                    emptyBarrelMapper.insertUseGeneratedKeys(emptyBarrel);
                }
            }
        }

    }

    @Transactional(rollbackFor = Exception.class)
    public void delShipping(Shipping shipping) {

        EmptyBarrel emptyBarrel = new EmptyBarrel();
        emptyBarrel.setShippingId(shipping.getId());
        emptyBarrel.setEmptyType(2);

        emptyBarrelMapper.delete(emptyBarrel);

    }

    //存桶登记
    @Transactional(rollbackFor = Exception.class)
    public void addEmptyBarrel(List<EmptyBarrel> itemList,Long shopId,String shippingEmployeeId) {
        for (EmptyBarrel emptyBarrel:itemList) {
            Product product = productService.selectByPrimaryKey(emptyBarrel.getProductId());
            emptyBarrel.setCost(product.getCost());
            emptyBarrel.setEmptyType(1);
            emptyBarrel.setCreateDate(new Date());
            emptyBarrel.setName(product.getName());
            emptyBarrel.setSpec(product.getSpec());
            emptyBarrel.setUnit(product.getUnit());
            emptyBarrel.setThumbnail(product.getThumbnail());
            emptyBarrel.setSn(product.getSn());
            emptyBarrel.setShopId(shopId);
            emptyBarrel.setAudited(1);
            if (emptyBarrel.getMemo()==null) {
                emptyBarrel.setMemo("人工存桶登记");
            } else {
                emptyBarrel.setMemo("人工存桶登记>"+emptyBarrel.getMemo());
            }
            emptyBarrel.setQuantity(emptyBarrel.getQuantity().abs());
            emptyBarrel.setShippingEmployeeId(shippingEmployeeId);
            emptyBarrelMapper.insertUseGeneratedKeys(emptyBarrel);
        }

    }

    //欠桶登记
    @Transactional(rollbackFor = Exception.class)
    public void decEmptyBarrel(List<EmptyBarrel> itemList,Long shopId,String shippingEmployeeId) {
        for (EmptyBarrel emptyBarrel:itemList) {
            Product product = productService.selectByPrimaryKey(emptyBarrel.getProductId());
            emptyBarrel.setCost(product.getCost());
            emptyBarrel.setEmptyType(2);
            emptyBarrel.setCreateDate(new Date());
            emptyBarrel.setName(product.getName());
            emptyBarrel.setSpec(product.getSpec());
            emptyBarrel.setUnit(product.getUnit());
            emptyBarrel.setThumbnail(product.getThumbnail());
            emptyBarrel.setShopId(shopId);
            emptyBarrel.setSn(product.getSn());
            emptyBarrel.setAudited(1);
            if (emptyBarrel.getMemo()==null) {
                emptyBarrel.setMemo("人工欠桶登记");
            } else {
                emptyBarrel.setMemo("人工欠桶登记>"+emptyBarrel.getMemo());
            }
            emptyBarrel.setQuantity(BigDecimal.ZERO.subtract(emptyBarrel.getQuantity().abs()));
            emptyBarrel.setShippingEmployeeId(shippingEmployeeId);
            emptyBarrelMapper.insertUseGeneratedKeys(emptyBarrel);
        }

    }


    //入库登记，采购空桶
    @Transactional(rollbackFor = Exception.class)
    public void addStock(List<EmptyBarrel> itemList,Long shopId) {
        for (EmptyBarrel emptyBarrel:itemList) {
            Product product = productService.selectByPrimaryKey(emptyBarrel.getProductId());
            emptyBarrel.setCost(product.getCost());
            emptyBarrel.setEmptyType(0);
            emptyBarrel.setCreateDate(new Date());
            emptyBarrel.setName(product.getName());
            emptyBarrel.setSpec(product.getSpec());
            emptyBarrel.setUnit(product.getUnit());
            emptyBarrel.setThumbnail(product.getThumbnail());
            emptyBarrel.setShopId(shopId);
            emptyBarrel.setSn(product.getSn());
            emptyBarrel.setAudited(1);
            if (emptyBarrel.getMemo()==null) {
                emptyBarrel.setMemo("入库登记");
            } else {
                emptyBarrel.setMemo("入库登记>"+emptyBarrel.getMemo());
            }
            emptyBarrel.setQuantity(emptyBarrel.getQuantity().abs());
            emptyBarrelMapper.insertUseGeneratedKeys(emptyBarrel);
        }

    }


    //出库登记，退回厂家
    @Transactional(rollbackFor = Exception.class)
    public void decStock(List<EmptyBarrel> itemList,Long shopId) {
        for (EmptyBarrel emptyBarrel:itemList) {
            Product product = productService.selectByPrimaryKey(emptyBarrel.getProductId());
            emptyBarrel.setCost(product.getCost());
            emptyBarrel.setEmptyType(3);
            emptyBarrel.setCreateDate(new Date());
            emptyBarrel.setName(product.getName());
            emptyBarrel.setSpec(product.getSpec());
            emptyBarrel.setUnit(product.getUnit());
            emptyBarrel.setThumbnail(product.getThumbnail());
            emptyBarrel.setShopId(shopId);
            emptyBarrel.setSn(product.getSn());
            emptyBarrel.setAudited(1);
            if (emptyBarrel.getMemo()==null) {
                emptyBarrel.setMemo("出库登记");
            } else {
                emptyBarrel.setMemo("出库登记>"+emptyBarrel.getMemo());
            }
            emptyBarrel.setAudited(0);
            emptyBarrelMapper.insertUseGeneratedKeys(emptyBarrel);
        }

    }


    //退货
//    @Transactional(rollbackFor = Exception.class)
//    public void addReturn(Return returns) {
//        for (ReturnItem returnItem:returns.getItemList()) {
//            if (returnItem.getSubType().equals(4)) {
//                EmptyBarrel emptyBarrel = new EmptyBarrel();
//                emptyBarrel.setAdminFreight(returnItem.getAdminFreight());
//                emptyBarrel.setShippingFreight(returnItem.getShippingFreight());
//                emptyBarrel.setLevelFreight(returnItem.getLevelFreight());
//                emptyBarrel.setCost(returnItem.getCost());
//                emptyBarrel.setProductId(returnItem.getProductId());
//                emptyBarrel.setEmptyType(2);
//                emptyBarrel.setCreateDate(new Date());
//                emptyBarrel.setEnterpriseId(returns.getEnterpriseId());
//                emptyBarrel.setMemberId(returns.getMemberId());
//                emptyBarrel.setName(returnItem.getName());
//                emptyBarrel.setSpec(returnItem.getSpec());
//                emptyBarrel.setUnit(returnItem.getUnit());
//                emptyBarrel.setThumbnail(returnItem.getThumbnail());
//                emptyBarrel.setSn(returnItem.getSn());
//                emptyBarrel.setReturnId(returns.getId());
//                emptyBarrel.setOrderId(returns.getOrderId());
//                emptyBarrel.setOrderSn(returns.getOrderSn());
//                emptyBarrel.setQuantity(returnItem.getQuantity());
//                emptyBarrel.setShopId(returns.getShopId());
//                emptyBarrel.setShippingEmployeeId(returns.getShippingEmployeeId());
//                emptyBarrel.setAudited(1);
//                emptyBarrel.setWarehouseId(returns.getWarehouseId());
//                emptyBarrelMapper.insertUseGeneratedKeys(emptyBarrel);
//
//                if (returnItem.getEmptyBarrelQuantity().compareTo(BigDecimal.ZERO)>0) {
//                    emptyBarrel.setAdminFreight(returnItem.getAdminFreight());
//                    emptyBarrel.setShippingFreight(returnItem.getShippingFreight());
//                    emptyBarrel.setLevelFreight(returnItem.getLevelFreight());
//                    emptyBarrel.setCost(returnItem.getCost());
//                    emptyBarrel.setProductId(returnItem.getProductId());
//                    emptyBarrel.setEmptyType(1);
//                    emptyBarrel.setCreateDate(new Date());
//                    emptyBarrel.setEnterpriseId(returns.getEnterpriseId());
//                    emptyBarrel.setMemberId(returns.getMemberId());
//                    emptyBarrel.setName(returnItem.getName());
//                    emptyBarrel.setSpec(returnItem.getSpec());
//                    emptyBarrel.setUnit(returnItem.getUnit());
//                    emptyBarrel.setThumbnail(returnItem.getThumbnail());
//                    emptyBarrel.setSn(returnItem.getSn());
//                    emptyBarrel.setReturnId(returns.getId());
//                    emptyBarrel.setOrderId(returns.getOrderId());
//                    emptyBarrel.setOrderSn(returns.getOrderSn());
//                    emptyBarrel.setQuantity(BigDecimal.ZERO.subtract(returnItem.getEmptyBarrelQuantity()));
//                    emptyBarrel.setShopId(returns.getShopId());
//                    emptyBarrel.setShippingEmployeeId(returns.getShippingEmployeeId());
//                    emptyBarrel.setWarehouseId(returns.getWarehouseId());
//                    emptyBarrel.setAudited(0);
//                    emptyBarrelMapper.insertUseGeneratedKeys(emptyBarrel);
//
//                }
//            }
//        }
//
//    }


    //回桶确认
    @Transactional(rollbackFor = Exception.class)
    public void audited(List<EmptyBarrel> itemList) {
        for (EmptyBarrel emptyBarrel:itemList) {
            emptyBarrel.setAudited(1);
            emptyBarrelMapper.updateByPrimaryKeySelective(emptyBarrel);
            ShippingItem shippingItem = shippingItemService.selectByPrimaryKey(emptyBarrel.getShippingItemId());
            if (shippingItem!=null) {
                shippingItem.setEmptyBarrelQuantity(emptyBarrel.getQuantity());
                shippingItemService.updateByPrimaryKeySelective(shippingItem);
            }
        }

    }

}
